﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Work.BL.Managers;
using Work.UI.Web.MVC.Code.Common;
using Work.UI.Web.MVC.Models.Resume;
using Work.BL.POCOs;

namespace Work.UI.Web.MVC.Controllers
{
    public class ResumeController : BaseController
    {
        #region Fields

        private readonly IResumeManager _resumeManager;
        private readonly IEducationManager _educationManager;
        private readonly IExpirianceManager _expirianceManager;
        private readonly ISectionManager _sectionManager;

        private static ResumePOCO _resume;
        private static EducationPOCO _education;
        private static ExpiriancePOCO _expiriance;

        private static bool _noExpiriance;

        #endregion

        #region Constructors

        public ResumeController(IResumeManager resumeManager, ISectionManager sectionManager,
            IExpirianceManager expirianceManager, IEducationManager educationManager)
        {
            _educationManager = educationManager;
            _expirianceManager = expirianceManager;
            _resumeManager = resumeManager;
            _sectionManager = sectionManager;
        }

        #endregion

        #region Web Methods Resume (Step 1)

        [HttpGet]
        public ActionResult Resume()
        {
            ViewBag.Section = _sectionManager.GetSections();
            return View(new ResumeModel());
        }

        [HttpPost]
        public ActionResult Resume(ResumeModel resumeModel)
        {
            if (ModelState.IsValid)
            {
                AddResumeValid(resumeModel);
                
                return RedirectToAction("Education");
            }
            ViewBag.Section = _sectionManager.GetSections();
            return View(new ResumeModel());
        }

        #endregion 

        #region Web Methods Education (Step 2)

        [HttpGet]
        public ActionResult Education()
        {
            if(_resume == null)
            {
                return RedirectToAction("Resume");
            }
            return View(new EducationModel());
        }

        [HttpPost]
        public ActionResult Education(EducationModel educationModel)
        {
            if (ModelState.IsValid)
            {
                AddEducationValid(educationModel);
                return RedirectToAction("Expiriance");
            }
            return View(new EducationModel());
        }

        #endregion

        #region Web Methods Expiriance (Step 3)

        [HttpGet]
        public ActionResult Expiriance()
        {
            if (_education == null)
            {
                return RedirectToAction("Education");
            }
            return View(new ExpirianceModel());
        }

        [HttpPost]
        public ActionResult Expiriance(ExpirianceModel expirianceModel)
        {
            _noExpiriance = expirianceModel.NoExpiriance;

            if (ModelState.IsValid || _noExpiriance)
            {
                AddExpirianceValid(expirianceModel);
                AddResume();
            }
            return View(new ExpirianceModel());
        }

        #endregion

        #region Helpers

        private void AddResumeValid(ResumeModel model)
        {
            _resume =new ResumePOCO();
            _resume.IdSection = model.IdSection;
            _resume.IsVisible = model.IsVisible;
            _resume.Language = model.Language;
            _resume.Salary = model.Salary;
            _resume.Title = model.Title;
            _resume.Description = model.Description;
            _resume.DateChanges = DateTime.Now;
        }

        private void AddEducationValid(EducationModel model)
        {
            _education = new EducationPOCO();
            _education.City = model.City;
            _education.Degree = model.Degree;
            _education.EndYear = model.EndYear;
            _education.Institution = model.Institution;
            _education.Speciality = model.Speciality;
            _education.StartYear = model.StartYear;
        }

        private void AddExpirianceValid(ExpirianceModel model)
        {
            _expiriance = new ExpiriancePOCO();
            _expiriance.Description = model.DescriptionExpiriance;
            _expiriance.EndDate = model.EndDate;
            _expiriance.Post = model.Post;
            _expiriance.ScopeOfActivation = model.ScopeOfActivation;
            _expiriance.StartDate = model.StartDate;
            _expiriance.Title = model.TitleExpiriance;
        }

        private void AddResume()
        {
            if (_resume != null && _education != null)
            {
                _resume = _resumeManager.Insert(_resume);
                _education.IdResume = _resume.Id;
                _educationManager.Insert(_education);

                if (!_noExpiriance)
                {
                    _expiriance.IdResume = _resume.Id;
                    _expirianceManager.Insert(_expiriance);
                }

                _resume = null;
                _education = null;
                _expiriance = null;
                _noExpiriance = false;
            }
            else
            {
                RedirectToAction("Resume");
            }
            
        }

        #endregion

    }
}
